package com.eding.controller.admin;

import com.eding.constant.JwtClaimsConstant;
import com.eding.dto.EmployeeDTO;
import com.eding.dto.EmployeeLoginDTO;
import com.eding.dto.EmployeePageQueryDTO;
import com.eding.dto.PasswordEditDTO;
import com.eding.entity.Employee;
import com.eding.properties.JwtProperties;
import com.eding.result.PageResult;
import com.eding.result.Result;
import com.eding.service.EmployeeService;
import com.eding.utils.JwtUtil;
import com.eding.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @ApiOperation(value = "员工登录")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @ApiOperation(value = "员工登出")
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工
     * @param employeeDTO
     * @return
     */
    @ApiOperation(value = "新增员工")
    @PostMapping
    public Result saveEmp(@RequestBody EmployeeDTO employeeDTO){
        log.info("新增员工：{}", employeeDTO);
        employeeService.saveEmp(employeeDTO);
        return Result.success();
    }

    /**
     * 员工分页查询
     * @param queryDTO
     * @return
     */
    @ApiOperation(value = "员工分页查询")
    @GetMapping("/page")
    public Result<PageResult> getEmpPage(EmployeePageQueryDTO queryDTO){
        PageResult pageResult = employeeService.getEmpPage(queryDTO);
        return Result.success(pageResult);
    }

    /**
     * 启用/禁用 员工账号
     * @param status
     * @param id
     * @return
     */
    @ApiOperation(value = "启用/禁用 员工账号")
    @PostMapping("status/{status}")
    public Result updateStatus(@PathVariable("status") Integer status, Long id){
        employeeService.updateStatus(status,id);
        return Result.success();
    }

    /**
     * 获取员工信息（用于修改页面回显）
     * @param id
     * @return
     */
    @ApiOperation(value = "获取员工信息")
    @GetMapping("/{id}")
    public Result<Employee> getEmpById(@PathVariable("id") Long id){
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }

    /**
     * 修改员工信息
     * @param employeeDTO
     * @return
     */
    @ApiOperation(value = "修改员工信息")
    @PutMapping
    public Result updateEmp(@RequestBody EmployeeDTO employeeDTO){
        employeeService.updateEmp(employeeDTO);
        return Result.success();
    }

    /**
     * 修改员工密码
     * @param passwordEditDTO
     * @return
     */
    @ApiOperation(value = "修改员工密码")
    @PutMapping("/editPassword")
    public Result editPassword(@RequestBody PasswordEditDTO passwordEditDTO){
        boolean checkOldPsd = employeeService.editPassword(passwordEditDTO);
        if (checkOldPsd == false){
            return Result.error("旧密码输入有误，请核实...");
        }
        return Result.success();
    }
}
